package com.hihiwjc.app.zzmh.apis;

import android.support.annotation.NonNull;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.callback.RequestCallBack;

/**
 * <br/>Created by hihiwjc on 2015/10/28 0028
 * <br/>Author:hihiwjc
 * <br/>Email:hihiwjc@live.com
 */

public abstract class BaseJSONApi extends RequestCallBack<String> {
    private int api_id = -1;
    private int action_id = -1;
    private ApiCallback mApiCallback;

    public BaseJSONApi(@NonNull ApiCallback apiCallback, int api_id) {
        if (api_id <= 0) {
            throw new IllegalStateException("api_id must more than 0.");
        }
        this.api_id = api_id;
        mApiCallback = apiCallback;
    }

    protected void setAction_id(int action_id) {
        if (api_id <= 0) {
            throw new IllegalStateException("action_id must more than 0.");
        }
        this.action_id = action_id;
    }

    public int apiId() {
        return api_id;
    }

    public int actionId() {
        return action_id;
    }

    @Override
    public void onFailure(HttpException error, String msg) {
        onCallback(null);
    }

    protected void onCallback(Object data) {
        mApiCallback.onApiCallback(genApiResult(data));
    }

    public ApiResult genApiResult(Object data) {
        if (api_id <= 0 || action_id <= 0) {
            throw new IllegalStateException("api_id or action_id must modify  manually.");
        }
        ApiResult apiResult = new ApiResult();
        apiResult.api_id=api_id;
        apiResult.action_id = action_id;
        apiResult.data = data;
        return apiResult;
    }
}
